' Strict
SuperStrict

' Modules
Import BRL.Max2D
'Import BtbN.GLDraw2D
Import BRL.FreeTypeFont
Import BRL.Retro
Import PUB.FreeJoy
Import BRL.Audio
Import BRL.OpenALAudio
Import BRL.FreeAudioAudio

?Win32
Import BRL.D3D7Max2D
Import dbs.d3d9max2d
?

' Files
Import "../TGame.bmx"
Import "../TINILoader.bmx"
Import "../Utilities/System.bmx"

' Global
Global gsInit:TGameStateInit

' TGameStateInit
Type TGameStateInit Extends TGameState
	' Init
	Method Init()
		' Title
		AppTitle = "Lyphia"
		
		' Mode
		?Debug
			game.logger.Write("Debug mode")
		?Not Debug
			game.logger.Write("Release mode")
		?
		
		' Load configuration
		Self.LoadConfiguration(FS_ROOT + "config.ini")
		
		' Init graphics mode
		game.logger.Write("Init graphics: " + game.driver + ": " + game.gfxWidth + " x " + game.gfxHeight)
		
		' Select graphics driver
		If game.driver = "Direct3D9"
			SetGraphicsDriver D3D9Max2DDriver() 
		ElseIf game.driver = "Direct3D7"
			SetGraphicsDriver D3D7Max2DDriver()
		Else
			SetGraphicsDriver GLMax2DDriver() 
		EndIf
		
		' Create window
		Graphics game.gfxWidth, game.gfxHeight, game.fullscreen * 32, 75
		
		' Seed
		SeedRnd MilliSecs()
		
		' Audio drivers
		Local audDrivers:String[] = AudioDrivers()
		game.logger.Write("Audio drivers:")
		For Local audDriver:String = EachIn audDrivers
			game.logger.Write(" * " + audDriver)
		Next
		
		' Init audio driver
		If AudioDriverExists(game.audioDriver)
			game.logger.Write("Init audio driver '" + game.audioDriver + "'")
			SetAudioDriver game.audioDriver
		Else
			game.logger.Write("Audio driver '" + game.audioDriver + "' does not exist")
		EndIf
		
		' Joysticks
		Local countGamePads:Int = JoyCount()
		game.logger.Write("Number of gamepads: " + countGamePads)
		game.logger.Write(TLog.separator)
		For Local i:Int = 0 To countGamePads - 1
			game.logger.Write("GamePad [" + (i + 1) +"]:")
			game.logger.Write(" * Name:       " + JoyName(i))
			game.logger.Write(" * AxisCaps:   " + Bin(JoyAxisCaps(i)))
			game.logger.Write(" * ButtonCaps: " + Bin(JoyButtonCaps(i)))
			game.logger.Write(" * X:          " + JoyX(i))
			game.logger.Write(" * Y:          " + JoyY(i))
			game.logger.Write(TLog.separator)
		Next
		
		' System info
		?Win32
			game.logger.Write("OS version: " + GetOSVersion())
			game.logger.Write("Number of processors: " + GetProcessorCount())
			game.logger.Write("Processor architecture: " + GetProcessorArchitecture())
		?
		
		' Hide mouse
		HideMouse
	End Method
	
	' Update
	Method Update()
		
	End Method
	
	' Remove
	Method Remove()
		
	End Method
	
	' LoadConfiguration
	Method LoadConfiguration(file:String)
		' Read config file
		Local ini:TINI = TINI.Create(file)
		ini.Load()
		
		' Size of the window
		game.SetVideoMode(Int(ini.Get("Graphics", "Width")), Int(ini.Get("Graphics", "Height")), Int(ini.Get("Graphics", "Fullscreen")), ini.Get("Graphics", "Driver"))
		game.vsync = Int(ini.Get("Graphics", "VSync"))
		game.audioDriver = ini.Get("Audio", "Driver")
	End Method
	
	' ToString
	Method ToString:String()
		Return "Init"
	End Method
	
	' Create
	Function Create:TGameStateInit(gameRef:TGame)
		Local gs:TGameStateInit = New TGameStateInit
		gameRef.RegisterGameState("Init", gs)
		Return gs
	End Function
End Type
